<?php
/**
 * Zead
 *
 * LICENSE	
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to eu@marcelomx.com so we can send you a copy immediately.
 *
 * @category   Zead
 * @author     Marcelo Rodrigues Gonzaga <eu@marcelomx.com>
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga
 * @link       http://code.google.com/p/zead 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 *
 **/


/** @see Zead_Manifest **/
require_once 'Zead/Manifest.php';

/**
 * Class Zead_Manifest_Adapter_Xml
 *
 * @category   Zead   Zead
 * @package    Zead_Manifest
 * @subpackage Zead_Manifest_Adapter
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License    
 */
class Zead_Manifest_Adapter_Xml extends Zead_Manifest
{
	protected function _setup()
	{
		$args = func_get_args();
		$fileName = $args[0];
		
		$manifest = new SimpleXmlElement(file_get_contents($fileName));
		
		// Process contents
		if ($manifest->contents) {
			// Parse contents attributes
			foreach ($manifest->contents->attributes() as $attr => $value) {
				$this->_contents->$attr = (string) $value;
			}
			// Parse contents list
			$this->_processContents($manifest->contents);
		}
		
		// Process options
		if ($manifest->options) {
			$this->_processOptions($manifest->options);
		}
		
		// Process quizzes
		if ($manifest->quizzes) {
			foreach ($manifest->quizzes->children() as $node) {
				if ($node->getName() != 'quiz') continue;
				$quiz = $this->createQuiz();
				
				// Set quiz title
				if ($node->title) {
					$quiz->title = (string) $node->title;
				}
				// Set quiz attrs
				foreach ($node->attributes() as $attr => $value) {
					$quiz->$attr = (string) $value;
				}
				// Parse questions
				if ($node->questions) {
					foreach ($node->questions->children() as $nodeQuestion) {
						if ($nodeQuestion->getName() != 'question') continue;
						$question = $this->createQuizQuestion();
						
						foreach ($nodeQuestion->attributes() as $attr => $value) {
							$question->$attr = (string) $value;
						}
						if ($nodeQuestion->title) {
							$question->title = (string) $nodeQuestion->title;
						}
						if ($nodeQuestion->description) {
							$question->description = (string) $nodeQuestion->description;
						}
						$quiz->append($question);
					}
				}
				// Append quiz
				$this->appendQuiz($quiz);
			}
		}
	}
	
	protected function _processContents($xmlObject, $parentContent = null)
	{
		foreach ($xmlObject->children() as $node) {
			if ($node->getName() != 'content') continue;
			
			$content = $this->createContent();
			
			foreach($node->attributes() as $attr => $value) {
				$value = (string) $value;
				$content->$attr = $value;
			}
			if ($node->title) {
				$content->title = (string) $node->title;
			}
			$this->appendContent($content, $parentContent);
			
			if ($node->children()) {
				$this->_processContents($node, $content);
			}
		}
	}
	
	protected function _processOptions($xmlObject, $parentOption = null)
	{	
		foreach ($xmlObject->children() as $node) {			
			$option = $this->createOption();			
			$option->name = $node->getName();			
						
			if (!count($node->children())) {
				// Set option value
				$value = (string) $node;
				
				if (preg_match("/^(int(eger)?|float|number|string|bool(ean)?)$/",($type = (string) $node['type']))) {
					$option->type = $type;
					settype($value, $type);
				}
				$option->value = $value;
			} else {
				$this->_processOptions($node, $option);
			}
			// Append option
			$this->appendOption($option, $parentOption);			
		}					
	}	
}
